//
//  ADView.swift
//  SinaNews
//
//  Created by hroot on 15/8/17.
//  Copyright (c) 2015年 Lily. All rights reserved.
//

import UIKit

class ADView: UIView, UIScrollViewDelegate {

    var dataArr: NSArray?
    var scrollView: UIScrollView?
    var pageControl: UIPageControl?
    var kScreenWidth = CGFloat(UIScreen.mainScreen().bounds.width)
    
//    override func init(frame: CGRect) {
//        
//    }
    func creatView(arr: NSArray) {
        self.scrollView = UIScrollView(frame: CGRectMake(8, 0, kScreenWidth-16, 170))
        for (var i = 0; i < arr.count; i++) {
            var floatI = CGFloat(i)
            var imageView = UIImageView(frame: CGRectMake((kScreenWidth-16) * floatI, 0, kScreenWidth-16, 150))
            var model = arr[i] as! NewsModel
            imageView.sd_setImageWithURL(NSURL(string: model.kpic!), placeholderImage: UIImage(named: "icon"))
            self.scrollView?.addSubview(imageView)
            
            var label = UILabel(frame: CGRectMake(0, 150, kScreenWidth-16, 20))
            label.text = model.title
            label.textColor = UIColor.whiteColor()
            label.backgroundColor = UIColor.blackColor()
            imageView.addSubview(label)
        }
        var count = CGFloat(arr.count)
        self.scrollView?.contentSize = CGSizeMake(count * kScreenWidth-16, 170)
        self.scrollView?.showsVerticalScrollIndicator = false
        self.scrollView?.pagingEnabled = true
        self.scrollView?.delegate = self
        self.addSubview(self.scrollView!)
        
        self.pageControl = UIPageControl(frame: CGRectMake(0, 170-30-30, kScreenWidth, 30))
        self.pageControl?.numberOfPages = arr.count
        self.pageControl?.addTarget(self, action: "pageClick", forControlEvents: UIControlEvents.ValueChanged)
        self.addSubview(self.pageControl!)
    }

    func pageClick(page: UIPageControl) {
        var currentPageCount = CGFloat(page.currentPage)
        self.scrollView?.setContentOffset(CGPointMake((kScreenWidth-16) * currentPageCount, 0), animated: true)
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        var currentPageCount = Int(self.scrollView!.contentOffset.x / self.scrollView!.bounds.width)
        self.pageControl?.currentPage =  currentPageCount
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
